<template>
  <div class="layout-container">
    <el-header class="header">
      <h1>签到管理系统</h1>
    </el-header>
    
    <div class="main-container">
      <div 
        class="sidebar-container"
        :style="{ width: isCollapse ? '64px' : '200px' }"
      >
        <div class="collapse-switch" @click="toggleCollapse">
          <el-icon :size="20">
            <Expand v-if="isCollapse" />
            <Fold v-else />
          </el-icon>
        </div>

        <el-menu
          :default-active="activeMenu"
          class="admin-menu"
          :collapse="isCollapse"
          :collapse-transition="false"
          router
          unique-opened
        >
          <!-- 签到管理（嵌套路由） -->
          <el-sub-menu>
            <template #title>
              <el-icon><Monitor /></el-icon>
              <span>签到管理</span>
            </template>
            <el-menu-item index="/attendance">签到信息</el-menu-item>
            <el-menu-item index="/result">签到结果</el-menu-item>
          </el-sub-menu>
          <!-- 系统设置（独立路由） -->
          <el-menu-item index="/class">
            <el-icon><Setting /></el-icon>
            <span>班级管理</span>
          </el-menu-item>

          <!-- 系统设置（独立路由） -->
          <el-menu-item index="/settings">
            <el-icon><Setting /></el-icon>
            <span>系统设置</span>
          </el-menu-item>
        </el-menu>
      </div>

      <!-- 内容区域 -->
      <div class="content-container">
        <router-view v-slot="{ Component }">
          <transition name="fade" mode="out-in">
            <component :is="Component" />
          </transition>
        </router-view>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, computed } from 'vue'
import { useRoute } from 'vue-router'
import { Monitor, Setting, Expand, Fold } from '@element-plus/icons-vue'

const isCollapse = ref(false)
const route = useRoute()

// 自动计算激活菜单
const activeMenu = computed(() => {
  return route.matched[1]?.path || route.path
})

const toggleCollapse = () => {
  isCollapse.value = !isCollapse.value
}
</script>

<style scoped>
.layout-container {
  height: 100vh;
  display: flex;
  flex-direction: column;
}

.header {
  background: #002140;
  color: #fff;
  height: 60px;
  line-height: 20px;
  text-align: center;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.main-container {
  flex: 1;
  display: flex;
  overflow: hidden;
}

.sidebar-container {
  background: #002140;
  display: flex;
  flex-direction: column;
  transition: width 0.3s;
}

.content-container {
  flex: 1;
  padding: 20px;
  background: #f0f2f5;
  overflow: auto;
  transition: margin-left 0.3s;
}

/* 过渡动画 */
.fade-enter-active,
.fade-leave-active {
  transition: opacity 0.2s ease;
}

.fade-enter-from,
.fade-leave-to {
  opacity: 0;
}

.collapse-switch {
  height: 56px;
  min-height: 56px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  background: #002140;
  transition: all 0.3s;
  
  .el-icon {
    color: rgba(255, 255, 255, 0.8);
    &:hover {
      color: #fff;
    }
  }
}

.admin-menu {
  flex: 1;
  border-right: none;
  background: #002140;
  width: 100% !important;
}
/* 菜单项通用样式 */
:deep(.el-menu-item),
:deep(.el-sub-menu__title) {
  height: 48px !important;
  line-height: 48px !important;
  color: rgba(255, 255, 255, 0.65) !important;
  background: #002140;
  &:hover {
    color: #fff !important;
  }
  
  .el-icon {
    font-size: 18px;
    margin-right: 12px;
  }
}

/* 折叠状态下调整图标间距 */
:deep(.el-menu--collapse) {
  .el-icon {
    margin-right: 0 !important;
  }
}

/* 激活状态样式 */
:deep(.el-menu-item.is-active) {
  background: #001529 !important;
  color: #fff !important;
}
</style>